import ajax from "@/util/request";

import { ElMessageBox } from "element-plus";

import { reactive, ref } from "vue";

import type { FormInstance } from "element-plus";

/**
 * 返回一个方法，调用后获取增加组件所需要用到的属性和方法
 * @param modelName 模块名
 * @param queryURL 查询地址，默认为/${modelName}/queryList
 * @param deleteURL 删除地址，默认为`/${modelName}/delete`
 * @param pk 删除时要传递的主键，默认为`t_${modelName}_pk`
 * @returns 
 */
function setupList(modelName: string,
    queryURL = `/${modelName}/queryList`,
    deleteURL = `/${modelName}/delete`,
    pk = `t_${modelName}_pk`) {

    const myForm = ref<FormInstance>();

    const showObj = reactive<any>({});

    const updateData = ref<any>({});

    const formData = reactive<any>({
        rows: 20
    });

    const useObj = reactive<any>({

    });

    const tableData = reactive<any>([]);

    const count = ref<number>(0);

    /**
     * 将当前表单数据提交进行查询
     */
    function handleQuery(page: number) {
        formData.page = page;
        ajax(queryURL, { ...formData }).then(({ data }) => {
            // 查询成功，将结果数据赋值到tableData中
            if (data.status > 0) {
                // console.log("查询结果：",res.data.result_data);
                tableData.length = 0;
                tableData.push(...data.result_data);

                count.value = data.count;
            }
        });
    }

    /**
     * 点击重置按钮：
     * 清空表单，完成数据查询
     */
    function handleReset(myForm: FormInstance | undefined) {
        if (myForm) {
            console.log("重置功能！");
            myForm.resetFields();
        }
        formData.rows = 20;
        handleQuery(1);
    }

    function handleDelete(row: any) {
        ElMessageBox.confirm("确认删除该选项吗，删除后将不可恢复?", "删除确认", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning",
        }).then(() => {
            //  调用删除接口
            // console.log("我要删除数据：", row);
            const deleteData: any = {};
            deleteData[pk] = row[pk];
            ajax(deleteURL, deleteData).then(
                ({ data }) => {
                    if (data.status > 0) {
                        alert({ message: "删除成功", type: "success" });
                        // 删除成功后，重置表单
                        handleReset(myForm.value);
                    }
                }
            );
        });
    }

    /**
     * 显示组件
     * @param obj 要显示的组件对象
     * @param row 
     */
    function handleShow(obj: any, row?: any) {
        for (const key in obj) {
            if (!showObj[key]) {
                showObj[key] = true;
            }
            if (!useObj[key]) {
                useObj[key] = true;
            }
        }
        if (row) {
            updateData.value = { ...row };
        }
    }

    /**
     *
     * @param rows 每页行数变化
     */
    function handleSizeChange(rows: number) {
        formData.rows = rows;
        handleQuery(1);
    }

    /**
     * 当前页数变化
     */
    function handleCurrentChange(page: number) {
        handleQuery(page);
    }

    /**
     * 关闭功能
     * @param updated 是否有新数据更新
     */
    function handleClose(updated: boolean) {
        for (const key in showObj) {
            showObj[key] = false;
        }
        if (updated) {
            handleReset(myForm.value);
        }
    }

    // 返回对象
    return {
        myForm, formData, updateData, tableData, count, showObj, useObj, handleClose,
        handleQuery, handleReset, handleDelete,
        handleShow, handleSizeChange, handleCurrentChange
    }
}

export default setupList;